package com.psycho.sys.api.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.psycho.sys.api.common.Result;
import com.psycho.sys.api.entity.User;
import com.psycho.sys.api.service.AdminService;
import com.psycho.sys.api.vo.AppointmentVO;
import com.psycho.sys.api.vo.ConsultationRecordVO;
import com.psycho.sys.api.vo.CounselorVO;
import com.psycho.sys.api.vo.StatisticsVO;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 管理员控制器
 */
@Tag(name = "管理员接口")
@RestController
@RequestMapping("/api/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    // 用户管理
    @Operation(summary = "分页查询用户列表")
    @GetMapping("/users/page")
    public Result<Page<User>> pageUsers(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "用户名") @RequestParam(required = false) String username,
            @Parameter(description = "真实姓名") @RequestParam(required = false) String realName,
            @Parameter(description = "用户类型") @RequestParam(required = false) Integer userType,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status) {
        return Result.success(adminService.pageUsers(current, size, username, realName, userType, status));
    }

    // 咨询师管理
    @Operation(summary = "分页查询咨询师列表")
    @GetMapping("/counselors/page")
    public Result<Page<CounselorVO>> pageCounselors(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "姓名") @RequestParam(required = false) String realName,
            @Parameter(description = "专业领域") @RequestParam(required = false) String specialization,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status) {
        return Result.success(adminService.pageCounselors(current, size, realName, specialization, status));
    }

    @Operation(summary = "审核咨询师资质")
    @PutMapping("/counselors/{id}/verify")
    public Result<Boolean> verifyCounselor(
            @PathVariable("id") Long counselorId,
            @Parameter(description = "审核状态（1-通过，2-拒绝）") @RequestParam Integer verifyStatus,
            @Parameter(description = "审核意见") @RequestParam(required = false) String verifyRemark) {
        return Result.success(adminService.verifyCounselor(counselorId, verifyStatus, verifyRemark));
    }

    // 预约管理
    @Operation(summary = "分页查询预约列表")
    @GetMapping("/appointments/page")
    public Result<Page<AppointmentVO>> pageAppointments(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "用户ID") @RequestParam(required = false) Long userId,
            @Parameter(description = "咨询师ID") @RequestParam(required = false) Long counselorId,
            @Parameter(description = "预约状态") @RequestParam(required = false) Integer status) {
        return Result.success(adminService.pageAppointments(current, size, userId, counselorId, status));
    }

    // 咨询记录管理
    @Operation(summary = "分页查询咨询记录")
    @GetMapping("/records/page")
    public Result<Page<ConsultationRecordVO>> pageRecords(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "用户ID") @RequestParam(required = false) Long userId,
            @Parameter(description = "咨询师ID") @RequestParam(required = false) Long counselorId,
            @Parameter(description = "记录状态") @RequestParam(required = false) Integer status) {
        return Result.success(adminService.pageRecords(current, size, userId, counselorId, status));
    }

    // 统计报表
    @Operation(summary = "获取系统统计数据")
    @GetMapping("/statistics")
    public Result<StatisticsVO> getStatistics(
            @Parameter(description = "开始日期") @RequestParam String startDate,
            @Parameter(description = "结束日期") @RequestParam String endDate) {
        return Result.success(adminService.getStatistics(startDate, endDate));
    }

    // 内容管理
    @Operation(summary = "发布系统公告")
    @PostMapping("/announcements")
    public Result<Boolean> publishAnnouncement(
            @Parameter(description = "公告标题") @RequestParam String title,
            @Parameter(description = "公告内容") @RequestParam String content,
            @Parameter(description = "公告类型") @RequestParam Integer type) {
        return Result.success(adminService.publishAnnouncement(title, content, type));
    }
}